<?php
/**
 * @authors liuqinglin 
 * @email (2961181429@qq.com) 
 * @(https://my.oschina.net/shp32131)
 * @date  2016-12-03 15:59:58
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class Admin extends Admin_Controller {
    
    public function __construct()
    {
        parent::__construct();
        $userinfo = $this->session->userinfo;
        if(!isset($userinfo))
        {
            redirect('admin/login');
        }
        $this->load->model('admin_model');
    }

    //显示管理员列表
    public function index()
    {
        $arr = array('id','root','name','email','phone','add_time');
        $data['data'] = $this->admin_model->get_data($arr);
        $this->load->view('templates/header');
        $this->load->view('templates/menu');
		$this->load->view('admin_list',$data);
    }

    //加载添加管理员视图表单
    public function add()
    {
        $this->load->view('templates/header');
        $this->load->view('templates/menu');       
    	$this->load->view('admin_add');
    }

    //新增管理员信息插入数据库
    public function insert()
    {
    	$data = $this->input->post(array('name','email','phone'),true);
        if($this->admin_model->insert($data))
        {
            $this->success("添加管理员成功！",'admin/admin');
        }
        else
        {
            $this->failure('添加管理员失败！','admin/admin/add',5);
        }
    }

    //删除管理员
    public function delete()
    {
    	//获取要删除记录的id
        $id=$this->input->get('id');
        if(isset($id))
        {
            if($this->admin_model->delete_one('id',$id))
            {
                $this->success('删除成功！','admin/admin');
            }
            else
            {
                $this->failure('删除失败！！！','admin/admin');
            }
        } 	
    }

    //加载管理员信息编辑的表单视图
    public function edit()
    {
    	$id = $this->input->get('id');

    	$data = $this->admin_model->get_one('id',$id);
        
        $this->load->view('templates/header');
        $this->load->view('templates/menu');
    	$this->load->view('admin_edit',$data);
    }
    //更新修改后的管理员信息到数据库
    public function update()
    {
    	$data = $this->input->post(array('id','name','email','phone'),true);

    	$field['name'] = 'id';

    	$field['value'] = $this->input->post('field');

    	if($this->admin_model->update_one($data,$field))
        {
            $this->success('修改成功!','admin/admin');
        }
        else
        {
            $this->failure('修改失败!!!','admin/admin/edit');
        }
    }
}